/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

const { ListNode } = require("./common");

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var detectCycle = function (head) {

  let p = head
  let map = new Map()
  for (let i = 0; p; p = p.next) {
    if (map.has(p)) {
      // 已经有这个地址
      return map.get(p)
    } else {
      map.set(p, i)
      i++
    }
  }
  return null
};

function detectCycle(head) {
  let p = head
  while (p) {
    if (p.flag) {
      delete p.flag
      return p
    } else {
      p.flag = true
      p = p.next
    }
  }
  return null
}